#include <stdio.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "com.h"


/*
 *
 */
void DrawComBkGnd(HWND hWnd)
{
	HDC hdc = GetClientDC(hWnd);
	RECT rect;
	GetClientRect(hWnd, &rect);
	int nWidth = GET_RECT_WIDTH(rect);
	int nHeight = GET_RECT_HEIGHT(rect);
	SetBrushColor(hdc, RGB2Pixel (hdc, 21, 111, 208));
	int nTop = CY_EDGE_BOTTOM - 5;
	FillBox(hdc, rect.left, rect.top, nWidth, nTop);
	int nBottom = CY_EDGE_BOTTOM - 5;
	FillBox(hdc, rect.left, rect.bottom - nBottom, nWidth, nBottom);
	SetBrushColor(hdc, RGB2Pixel (hdc, 73, 178, 230));
	FillBox(hdc, rect.left, rect.top + nTop, nWidth, nHeight - nTop - nBottom);
	SetBkMode(hdc, BM_TRANSPARENT);
	rect.bottom = rect.top + nTop;
	DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	ReleaseDC(hdc);
}
/*
 *
 */
void Draw3DRect(HDC hDC, int x0, int y0, int x1, int y1)
{
    SetPenColor(hDC, g_color[1]);
    MoveTo(hDC, x0, y1);
    LineTo(hDC, x0, y0);
    LineTo(hDC, x1, y0);
    SetPenColor(hDC, g_color[2]);
    LineTo(hDC, x1, y1);
    LineTo(hDC, x0, y1);
}
/*
 *
 */
void Draw3DBox(HDC hDC, int x0, int y0, int x1, int y1)
{
    SetPenColor(hDC, g_color[2]);
    MoveTo(hDC, x0, y1);
    LineTo(hDC, x0, y0);
    LineTo(hDC, x1, y0);
    SetPenColor(hDC, g_color[1]);
    LineTo(hDC, x1, y1);
    LineTo(hDC, x0, y1);
}
/*
 *
 */
void DrawBitmapBkGnd(HWND hWnd, RECT *prect, BITMAP *pbitmap)
{
    RECT rect;
    if(prect)
    {
        memcpy(&rect, prect, sizeof(RECT));
        ScreenToClient(hWnd, &rect.left, &rect.top);
        ScreenToClient(hWnd, &rect.right, &rect.bottom);
    }
    else
    {
        GetClientRect(hWnd, &rect);
    }
    HDC hdc = GetClientDC(hWnd);
    int nWidth = GET_RECT_WIDTH(rect);
    int nHeight = GET_RECT_HEIGHT(rect);
    FillBoxWithBitmapPart(hdc, rect.left, rect.top, nWidth, nHeight, 0, 0, pbitmap, rect.left, rect.top);
    ReleaseDC(hdc);
}
/*
 *
 */
void LoadMemBmp()
{
    int i = 0;
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_XTCS], GET_XTCS_WND_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_MAST], GET_MAST_WND_BKGND_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_HELP], GET_HELP_WND_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_ROCK_TRAN], GET_ROCK_TRAN_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_DMBZ], GET_DMBZ_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_YARN_CHANGE], GET_YARN_CHANGE_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_TIME], GET_TIME_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_HEAD_TEST], GET_HEAD_TEST_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_RUN], GET_RUN_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_RUN_SS], GET_RUN_SS_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_RAB], GET_RAB_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_YARN_STOP], GET_YARN_STOP_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_SINK], GET_SINK_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_YARN_CHECK], GET_YARN_CHECK_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_YARN_USED], GET_YARN_USED_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_YARN_NO_USED], GET_YARN_NO_USED_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_FLW], GET_FLW_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_PROGRAM], GET_PROGRAM_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_KEYPANE], GET_KEYPANE_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_KEYPANE_MAC], GET_KEYPANE_MAC_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_FILE], GET_FILE_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_0BIG], GET_BIG0_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_1BIG], GET_BIG1_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_2BIG], GET_BIG2_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_3BIG], GET_BIG3_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_4BIG], GET_BIG4_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_5BIG], GET_BIG5_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_6BIG], GET_BIG6_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_7BIG], GET_BIG7_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_8BIG], GET_BIG8_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_9BIG], GET_BIG9_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_DEL], GET_DEL_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_ESC], GET_ESC_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_ENTER], GET_ENTER_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_MOVE], GET_MOVE_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_ASK], GET_ASK_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_FALSE], GET_FALSE_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_TRUE], GET_TRUE_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_PASSWORD], GET_PASSWORD_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_ASK_DLG], GET_ASK_DLG_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_INFO], GET_INFO_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_INFO_LEN], GET_INFO_LEN_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_YES], GET_YES_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_NO], GET_NO_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_FILE_INPUT], GET_FILE_INPUT_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_COMPANY], GET_COMPANY_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_0DAT], GET_DAT_0BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_1DAT], GET_DAT_1BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_2DAT], GET_DAT_2BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_3DAT], GET_DAT_3BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_4DAT], GET_DAT_4BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_5DAT], GET_DAT_5BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_6DAT], GET_DAT_6BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_7DAT], GET_DAT_7BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_8DAT], GET_DAT_8BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_9DAT], GET_DAT_9BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_aDAT], GET_DAT_aBMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_bDAT], GET_DAT_bBMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_cDAT], GET_DAT_cBMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_dDAT], GET_DAT_dBMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_eDAT], GET_DAT_eBMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_fDAT], GET_DAT_fBMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_DAT_ESC], GET_DAT_ESC_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_DAT_ESC1], GET_DAT_ESC_BMP1());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_DAT_ENTER], GET_DAT_ENTER_BMP());
     LoadBitmap(HDC_SCREEN, &g_bmp[BMP_DAT_AUTO], GET_DAT_AUTO_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_COPY], GET_COPY_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_BOARD_TEST], GET_BOARD_TEST_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_LOG], GET_LOG_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_RETRY], GET_RETRY_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_HEX_DEL], GET_HEX_DEL_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_HEX_ESC], GET_HEX_ESC_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_HEX_ENTER], GET_HEX_ENTER_BMP());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_SAVE_SC], GET_SAVE_SCREEN());
    LoadBitmap(HDC_SCREEN, &g_bmp[BMP_FLW_FCS],GET_FLW_FOCUS());
    for(i = 0; i < MAX_BMP; ++i)
    {
        if(i != BMP_HEAD_TEST)
        {
            g_bmp[i].bmType = BMP_TYPE_COLORKEY;
            g_bmp[i].bmColorKey = RGB2Pixel(HDC_SCREEN, 255, 255, 255);
        }
    }
}
/*
 *
 */
void UnloadMemBmp()
{
    int i = 0;
    for(i = 0; i < MAX_BMP; ++i)
    {
        UnloadBitmap(&g_bmp[i]);
    }
}
/*
 *
 */
void DrawCross(HDC hDC, RECT *rect)
{
    int xMid = (rect->left + rect->right) / 2;
    int yMid = (rect->top + rect->bottom) / 2;
    SetPenColor(hDC, COLOR_HQ_RED(hDC));
    MoveTo(hDC, rect->left, yMid);
    LineTo(hDC, rect->right - 1, yMid);
    MoveTo(hDC, xMid, rect->top);
    LineTo(hDC, xMid, rect->bottom - 1);
}

